<template>
  <div class="app-container">

    <el-row>
      <!-- 部门树 -->
      <el-col :lg="3" :xs="24" style="">
        <el-card shadow="hover">
          <el-input v-model="groupName" placeholder="设备组名称" prefix-icon="Search" clearable/>
          <el-tree class="mt-2" ref="groupTreeRef" node-key="id" :data="groupOptions"
                   :props="{ label: 'groupName', children: 'children' }"
                   :expand-on-click-node="false"
                   :filter-node-method="filterNode"
                   highlight-current
                   default-expand-all
                   @node-click="handleNodeClick"/>
        </el-card>
      </el-col>

      <!--      设备编辑-->
      <el-col :lg="20" style="margin-left: 10px" v-if="groupLevel === 2">
        <!--        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">-->
        <!--          <el-form-item>-->
        <!--            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>-->
        <!--            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>-->
        <!--          </el-form-item>-->
        <!--        </el-form>-->

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5" v-if="chooseGroupId > 0">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
                       v-hasPermi="['module:terminals:add']">新增
            </el-button>
          </el-col>
          <el-col :span="1.5" v-if="chooseGroupId > 0">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddBulk"
                       v-hasPermi="['module:terminals:add']">批量新增
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                       v-hasPermi="['module:terminals:edit']">修改
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                       v-hasPermi="['module:terminals:remove']">删除
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" icon="el-icon-download" size="mini" :loading="exportLoading" @click="handleExport"
                       v-hasPermi="['module:terminals:export']">导出
            </el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="terminalsList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center"/>
          <el-table-column label="设备终端号" align="center" prop="terminalNum"/>
          <el-table-column label="设备名称" align="center" prop="terminalName"/>
          <!--          <el-table-column label="状态" align="center" prop="state">-->
          <!--            <template slot-scope="scope">-->
          <!--              <div>-->
          <!--                <el-tag v-if="scope.row.state === 0" style="cursor: pointer" :type="'info'">停用</el-tag>-->
          <!--                <el-tag v-if="scope.row.state === 1" style="cursor: pointer" :type="''">正常</el-tag>-->
          <!--                <el-tag v-if="scope.row.state === 2" style="cursor: pointer" :type="'danger'">报修</el-tag>-->
          <!--              </div>-->
          <!--            </template>-->
          <!--          </el-table-column>-->
          <el-table-column label="状态" align="center" key="state">
            <template slot-scope="scope">
              <el-switch v-model="scope.row.state"
                         :active-value="1"
                         :inactive-value="0"
                         @change="handleChangeSaleOut(scope.row)"></el-switch>
            </template>
          </el-table-column>
          <el-table-column label="二维码下载" align="center" width="150">
            <template slot-scope="scope">
              <el-popover placement="right" width="400" trigger="hover">
                <el-image style="width: 380px; height: 380px" :src="scope.row.qrCode" fit="fill"/>
                <i class="el-icon-picture" slot="reference"/>
              </el-popover>
              <el-button size="mini" type="text" icon="el-icon-download"
                         @click="downloadImg(scope.row)">{{ scope.row.terminalName }}
              </el-button>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-picture" @click="resetQrCode(scope.row)"
                         v-hasPermi="['module:terminals:edit']">生成二维码
              </el-button>
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                         v-hasPermi="['module:terminals:edit']">修改
              </el-button>
              <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                         v-hasPermi="['module:terminals:remove']">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="total > 0"
                    :total="total"
                    :page.sync="queryParams.pageNum"
                    :limit.sync="queryParams.pageSize"
                    @pagination="getList"/>
      </el-col>


      <!--      设备组编辑-->
      <el-col :lg="20" style="margin-left: 10px" v-else>

        <el-form :model="queryParamsGroup" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="所属物业" prop="propertyId">
            <el-select v-model="queryParamsGroup.propertyId" placeholder="请选择物业" size="mini">
              <el-option v-for="property in propertyListOptions"
                         :key="property.id"
                         :label="property.propertyName"
                         :value="property.id"/>
            </el-select>
          </el-form-item>
          <el-form-item label="所属小区" prop="estateId">
            <el-select v-model="queryParamsGroup.estateId" placeholder="请选择小区" style="width:140px;" size="mini"
                       filterable clearable>
              <el-option v-for="estate in estatesOptions"
                         :key="estate.id"
                         :label="estate.estateName"
                         :value="estate.id"/>
            </el-select>
          </el-form-item>
          <el-form-item label="设备组" prop="groupName">
            <el-input v-model="queryParamsGroup.groupName" placeholder="请输入设备组名称" clearable size="mini"
                      @keyup.enter.native="handleQuery"/>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddGroup"
                       v-hasPermi="['module:terminalGroup:add']">新增
            </el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"/>
        </el-row>

        <el-table v-loading="loading" :data="groupList" row-key="id" default-expand-all
                  :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
          <el-table-column label="设备组名称" align="center" prop="groupName"/>
          <el-table-column label="物业公司" align="center" prop="propertyName"/>
          <el-table-column label="所属小区" align="center" prop="estateName"/>
          <el-table-column label="状态" align="center">
            <template slot-scope="scope">
              <div>
                <el-tag v-if="scope.row.state === 0" style="cursor: pointer" :type="'info'">停用</el-tag>
                <el-tag v-else style="cursor: pointer" :type=" '' ">正常</el-tag>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdateGroup(scope.row)"
                         v-hasPermi="['module:terminalGroup:edit']">修改
              </el-button>
              <el-button size="mini" type="text" icon="el-icon-plus" @click="handleAddGroup(scope.row)"
                         v-hasPermi="['module:terminalGroup:add']">新增
              </el-button>
              <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDeleteGroup(scope.row)"
                         v-hasPermi="['module:terminalGroup:remove']">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>

      </el-col>

    </el-row>

    <!-- 添加或修改巡检设备对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="11">
            <el-row>
              <el-col :span="22">
                <el-form-item label="所属物业" prop="propertyId">
                  <el-select v-model="form.propertyId" placeholder="请选择所属物业" size="mini" style="width:100%;"
                             @change="selectEstate">
                    <el-option v-for="property in propertyListOptions"
                               :key="property.id"
                               :label="property.propertyName"
                               :value="property.id"/>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="22">
                <el-form-item label="所属小区" prop="estateId">
                  <el-select v-model="form.estateId" placeholder="请选择小区" filterable clearable size="mini"
                             style="width:100%">
                    <el-option v-for="estate in estatesOptions"
                               :key="estate.id"
                               :label="estate.estateName"
                               :value="estate.id"/>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="22">
                <el-form-item label="内容模板" prop="surveyTemplateId">
                  <el-select v-model="form.surveyTemplateId" placeholder="请选择内容模板" filterable clearable
                             size="mini" style="width:100%">
                    <el-option v-for="su in surveyTemplateList"
                               :key="su.id"
                               :label="su.questionTitle"
                               :value="su.id"/>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="22">
                <el-form-item label="标准模板" prop="surveyStandardId">
                  <el-select v-model="form.surveyStandardId" placeholder="请选择内容模板" filterable clearable
                             size="mini" style="width:100%">
                    <el-option v-for="su in surveyStandardList"
                               :key="su.id"
                               :label="su.questionTitle"
                               :value="su.id"/>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="22">
                <el-form-item label="任务类型" prop="pointQrInfoId">
                  <el-select v-model="form.pointQrInfoId" placeholder="请选择通用点" filterable clearable size="mini"
                             style="width:100%">
                    <el-option v-for="qr in qrList"
                               :key="qr.id"
                               :label="qr.typeName"
                               :value="qr.id"/>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="22">
                <el-form-item label="设备终端号" prop="terminalNum">
                  <el-input v-model="form.terminalNum" placeholder="请输入设备终端号" size="mini"/>
                </el-form-item>
              </el-col>
              <el-col :span="22">
                <el-form-item label="设备名称" prop="terminalName">
                  <el-input v-model="form.terminalName" placeholder="请输入设备名称" size="mini"/>
                </el-form-item>
              </el-col>
              <el-col :span="22">
                <el-form-item label="位置信息" prop="address">
                  <el-input v-model="form.address" placeholder="请输入位置信息" size="mini"/>
                </el-form-item>
              </el-col>
              <el-col :span="22">
                <el-form-item label="状态" prop="state">
                  <el-radio-group v-model="form.state" size="mini">
                    <el-radio :value="0" :label="0">停用</el-radio>
                    <el-radio :value="1" :label="1">正常</el-radio>
                    <el-radio :value="2" :label="2">报修</el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>

              <el-col :span="22">
                <el-form-item label="相关介绍信息" prop="remark">
                  <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" size="mini"/>
                </el-form-item>
              </el-col>
            </el-row>
          </el-col>
          <el-col :span="13">
            <el-col :span="24">
              <div style="width:100%;height:400px;">
                <TianDiTu :zoom="12" :label-title="form.estateName"
                          :default-lat="parseFloat(form.lat)" :default-lng="parseFloat(form.lng)"
                          @pickLngLat="pickLngLat"></TianDiTu>
              </div>
            </el-col>
            <el-col :span="24">
              <el-form-item label="经纬度" prop="lng">
                <el-input v-model="form.lng" placeholder="请输入经度" style="width:47%" size="mini"/>
                <el-input v-model="form.lat" placeholder="请输入纬度" style="width:47%" size="mini"/>
              </el-form-item>
            </el-col>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" size="mini" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel" size="mini">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 添加或修改设备组对话框 -->
    <el-dialog :title="titleGroup" :visible.sync="openGroup" width="500px" append-to-body>
      <el-form ref="form" :model="formGroup" :rules="rules" label-width="90px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="所属层级" prop="parentId">
              <treeselect v-model="formGroup.parentId" :options="groupOptions" :normalizer="normalizer"
                          placeholder="请选择父层级id"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属物业" prop="propertyId">
              <el-select v-model="formGroup.propertyId" placeholder="请选择物业" style="width:100%;" size="mini"
                         filterable clearable @change="selectEstate">
                <el-option v-for="property in propertyListOptions"
                           :key="property.id"
                           :label="property.propertyName"
                           :value="property.id"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属小区" prop="estateId">
              <el-select v-model="formGroup.estateId" placeholder="请选择小区" style="width:100%;" size="mini"
                         filterable clearable>
                <el-option v-for="estate in estatesOptions"
                           :key="estate.id"
                           :label="estate.estateName"
                           :value="estate.id"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="设备组名称" prop="groupName">
              <el-input v-model="formGroup.groupName" placeholder="请输入设备组名称" size="mini"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="状态" prop="state">
              <el-radio-group v-model="formGroup.state" size="mini">
                <el-radio :value="1" :label="1">启用</el-radio>
                <el-radio :value="0" :label="0">停用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" size="mini" type="primary" @click="submitFormGroup">确 定</el-button>
        <el-button @click="cancelGroup" size="mini">取 消</el-button>
      </div>
    </el-dialog>

    <!--批量新增设备-->
    <el-dialog :title="title" :visible.sync="openGroupBulk" width="800px" append-to-body>
      <el-form ref="form" :model="formGroupBulk" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="22">
            <el-form-item label="所属物业" prop="propertyId">
              <el-select v-model="formGroupBulk.propertyId" placeholder="请选择所属物业" size="mini" style="width:100%;"
                         @change="selectEstate">
                <el-option v-for="property in propertyListOptions"
                           :key="property.id"
                           :label="property.propertyName"
                           :value="property.id"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="所属小区" prop="estateId">
              <el-select v-model="formGroupBulk.estateId" placeholder="请选择小区" filterable clearable size="mini"
                         style="width:100%">
                <el-option v-for="estate in estatesOptions"
                           :key="estate.id"
                           :label="estate.estateName"
                           :value="estate.id"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="内容模板" prop="surveyTemplateId">
              <el-select v-model="formGroupBulk.surveyTemplateId" placeholder="请选择内容模板" filterable clearable
                         size="mini" style="width:100%">
                <el-option v-for="su in surveyTemplateList"
                           :key="su.id"
                           :label="su.questionTitle"
                           :value="su.id"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="标准模板" prop="surveyStandardId">
              <el-select v-model="formGroupBulk.surveyStandardId" placeholder="请选择内容模板" filterable clearable
                         size="mini" style="width:100%">
                <el-option v-for="su in surveyStandardList"
                           :key="su.id"
                           :label="su.questionTitle"
                           :value="su.id"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="任务类型" prop="pointQrInfoId">
              <el-select v-model="formGroupBulk.pointQrInfoId" placeholder="请选择通用点" filterable clearable
                         size="mini" style="width:100%">
                <el-option v-for="qr in qrList"
                           :key="qr.id"
                           :label="qr.typeName"
                           :value="qr.id"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="前置终端号" prop="terminalNum">
              <el-input v-model="formGroupBulk.terminalNum" placeholder="请输入设备终端号" size="mini"/>
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="起始编号" prop="terminalNumBegin">
              <el-input v-model="formGroupBulk.terminalNumBegin" placeholder="请输入设备终端号" size="mini"/>
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="结束编号" prop="terminalNumEnd">
              <el-input v-model="formGroupBulk.terminalNumEnd" placeholder="请输入设备终端号" size="mini"/>
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="设备名称" prop="terminalName">
              <el-input v-model="formGroupBulk.terminalName" placeholder="请输入设备名称" size="mini"/>
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="位置信息" prop="address">
              <el-input v-model="formGroupBulk.address" placeholder="请输入位置信息" size="mini"/>
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="状态" prop="state">
              <el-radio-group v-model="formGroupBulk.state" size="mini">
                <el-radio :value="0" :label="0">停用</el-radio>
                <el-radio :value="1" :label="1">正常</el-radio>
                <el-radio :value="2" :label="2">报修</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>

          <el-col :span="22">
            <el-form-item label="相关介绍信息" prop="remark">
              <el-input v-model="formGroupBulk.remark" type="textarea" placeholder="请输入内容" size="mini"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" size="mini" type="primary" @click="submitFormGroupBulk">确 定</el-button>
        <el-button @click="cancel" size="mini">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  listTerminals,
  getTerminals,
  delTerminals,
  addTerminals,
  updateTerminals,
  exportTerminals,
  resetQrCode,
  updateTerminalState,
  addTerminalsBulk
} from "@/api/module/event/terminal/terminals";
import {addGroup, delGroup, getGroup, listGroup, updateGroup} from "@/api/module/event/terminal/group";
import {listPropertyNoPage} from "@/api/module/property/property";
import Template from "@/views/module/wx/template.vue";
import {listAllEstate} from "@/api/module/estate/estate";
import {listSurvey,} from "@/api/module/survey/survey";

import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {listQrs} from "@/api/module/event/point/qr";
import {listInfos} from "@/api/module/event/point/qrInfo";
import {stateSaleOutStatus} from "@/api/module/estate/user";

export default {
  name: "Terminals",
  components: {Treeselect, Template},
  data() {
    return {
      map: null,
      marker: null,
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      tipinput: '',
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      groupList: [],
      groupName: '',
      chooseGroupId: 0,
      groupLevel: 0,
      parentIds: undefined,
      // 巡检设备表格数据
      terminalsList: [],
      propertyListOptions: [],
      estatesOptions: [],
      surveyTemplateList: [],
      surveyStandardList: [],
      groupOptions: [],
      chooseGroupList: [],// 选中的 节点下的 设备组列表
      // 弹出层标题
      title: "",
      titleGroup: "",
      // 是否显示弹出层
      open: false,
      openGroup: false,
      openGroupBulk: false,
      qrList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        groupId: undefined,
      },
      // 查询参数
      queryParamsGroup: {
        pageNum: 1,
        pageSize: 10,
        parentId: undefined,
        estateId: undefined,
        propertyId: undefined,
        groupName: undefined,
      },
      queryParamsEstate: {
        propertyId: undefined,
      },
      queryParamsSurvey: {
        pageNum: 1,
        pageSize: 100,
        propertyId: undefined,
        estateId: undefined,
        surveyType: undefined,
        functionType: undefined,
      },
      queryParamQr: {
        eventType: 1,
      },
      // 表单参数
      form: {},
      formGroup: {},
      formGroupBulk: {},
      // 表单校验
      rules: {
        propertyId: [
          {required: true, message: "物业公司不能为空", trigger: "blur"}
        ],
        estateId: [
          {required: true, message: "所属小区不能为空", trigger: "blur"}
        ],
        // surveyTemplateId: [
        //   {required: true, message: "内容模板不能为空", trigger: "blur"}
        // ],
        // surveyStandardId: [
        //   {required: true, message: "标准模板不能为空", trigger: "blur"}
        // ],
        terminalName: [
          {required: true, message: "设备名称不能为空", trigger: "blur"}
        ],
        state: [
          {required: true, message: "设备状态不能为空", trigger: "blur"}
        ],
      }
    }
  },
  watch: {
    // 根据名称筛选部门树
    groupName(val) {
      this.$refs.groupTreeRef.filter(val);
    }
  },
  created() {
    this.getPropertyList();
    this.getEstateList();
    this.getList();
    this.getTreeSelect();
    this.getGroupList();
    this.getQrPointList();
  },
  methods: {
    /** 查询巡检设备列表 */
    getList() {
      this.loading = true;
      listTerminals(this.queryParams).then(response => {
        this.terminalsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询设备组列表 */
    getQrPointList() {
      listInfos(this.queryParamQr).then(response => {
        this.qrList = response;
        this.loading = false;
      });
    },
    pickLngLat(e) {
      console.log('getLngLat e === ', e)
      this.form.lng = e.lng.toFixed(6);
      this.form.lat = e.lat.toFixed(6);
    },
    getGroupList() {
      listGroup(this.queryParamsGroup).then(response => {
        this.groupList = this.handleTree(response.data, "id", "parentId");
      });
    },
    /** 查询设备组下拉树结构 */
    getTreeSelect() {
      this.queryParamsGroup.parentId = undefined;
      listGroup(this.queryParamsGroup).then(response => {
        this.groupOptions = [];
        const data = {id: 0, groupName: '主类目', children: []}
        data.children = this.handleTree(response.data, "id", "parentId");
        this.groupOptions.push(data);
        console.log(this.groupOptions)
      });
    },
    handleChangeSaleOut(row) {
      let text = row.state === 0 ? "启用" : "停用";
      this.$confirm('确认要修改"' + text + '"的状态吗?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return updateTerminalState(row.id, row.state);
      }).then(() => {
        this.msgSuccess("修改 " + text + "成功");
        this.getList();
      }).catch(function () {
        row.state = row.state === "0" ? "1" : "0";
      });
    },
    /** 转换设备组数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.groupName,
        children: node.children
      };
    },
    getPropertyList() {
      listPropertyNoPage().then(response => {
        this.propertyListOptions = response.data;
      });
    },
    getSurveyTemplateList() {
      this.queryParamsSurvey.surveyType = 1;
      this.queryParamsSurvey.functionType = 1;
      listSurvey(this.queryParamsSurvey).then(response => {
        this.surveyTemplateList = response.rows;
      });
    },
    getSurveyStandardList() {
      this.queryParamsSurvey.surveyType = 2;
      this.queryParamsSurvey.functionType = 1;
      listSurvey(this.queryParamsSurvey).then(response => {
        this.surveyStandardList = response.rows;
      });
    },

    selectEstate(val) {
      this.queryParamsEstate.propertyId = val;
      this.getEstateList();
    },
    getEstateList() {
      listAllEstate(this.queryParamsEstate).then(response => {
        console.log('getEstateList', response.data);
        this.estatesOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    /** 节点单击事件 */
    handleNodeClick(data) {
      console.log('handleNodeClick', data);
      this.chooseGroupId = data.id;
      this.groupLevel = data.level;
      if (this.groupLevel == 2) {
        this.form.groupId = data.id;
        this.queryParams.groupId = data.id;
        this.getList()
      } else {
        this.formGroup.parentId = data.id;
        this.parentIds = data.id;
        this.queryParamsGroup.parentId = data.id;
        this.getGroupList();
      }
    },
    downloadImg(row) {
      window.location.href = process.env.VUE_APP_BASE_API + "/common/download/terminal/" + row.id;
    },
    resetQrCode(row) {
      var data = {
        id: row.id
      };
      resetQrCode(data).then(response => {
        if (response.code === 200) {
          this.msgSuccess("生成成功");
          this.getList();
        } else {
          this.msgError("生成失败");
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openGroupBulk = false;
      this.reset();
    },
    // 取消按钮
    cancelGroup() {
      this.openGroup = false;
      this.resetGroup();
    },
    // 表单重置
    resetGroup() {
      this.formGroup = {
        id: undefined,
        propertyId: undefined,
        estateId: undefined,
        groupName: undefined,
        parentId: this.queryParamsGroup.parentId,
        level: undefined,
        state: undefined,
      }
      this.resetForm("form");
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        propertyId: undefined,
        estateId: undefined,
        groupId: this.chooseGroupId,
        surveyTemplateId: undefined,
        surveyStandardId: undefined,
        terminalNumEnd: undefined,
        terminalNumBegin: undefined,
        terminalNum: undefined,
        terminalName: undefined,
        terminalType: undefined,
        address: undefined,
        state: undefined,
        lng: undefined,
        lat: undefined,
        qrCode: undefined,
        createTime: undefined,
        remark: undefined,
        pointQrInfoId: undefined,
      }
      this.resetForm("form");
    },

    resetGroupBulk() {
      this.formGroupBulk = {
        id: undefined,
        propertyId: undefined,
        estateId: undefined,
        groupId: this.chooseGroupId,
        surveyTemplateId: undefined,
        surveyStandardId: undefined,
        terminalNumEnd: undefined,
        terminalNumBegin: undefined,
        terminalNum: undefined,
        terminalName: undefined,
        terminalType: undefined,
        address: undefined,
        state: undefined,
        lng: undefined,
        lat: undefined,
        qrCode: undefined,
        createTime: undefined,
        remark: undefined,
        pointQrInfoId: undefined,
      }
      this.resetForm("formGroupBulk");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getGroupList();
      this.getTreeSelect();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      let that = this;
      this.reset();
      this.getPropertyList();
      this.getSurveyTemplateList();
      this.getSurveyStandardList();
      this.open = true;
      this.title = "添加巡检设备";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      let that = this;
      this.loading = true;
      this.reset();
      this.getPropertyList();
      this.getSurveyTemplateList();
      this.getSurveyStandardList();
      const id = row.id || this.ids;

      getTerminals(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改巡检设备";
      });
    },
    submitFormGroupBulk() {
      this.buttonLoading = true;
      console.log(this.formGroupBulk);
      addTerminalsBulk(this.formGroupBulk).then(response => {
        this.msgSuccess("批量新增成功");
        this.openGroupBulk = false;
        this.getList();
        this.getTreeSelect();
        this.getGroupList();
      }).finally(() => {
        this.buttonLoading = false;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateTerminals(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
              this.getTreeSelect();
              this.getGroupList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addTerminals(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              this.getTreeSelect();
              this.getGroupList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除巡检设备编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.loading = true;
        return delTerminals(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.getTreeSelect();
        this.getGroupList();
        this.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有巡检设备数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportTerminals(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {
      });
    },
    handleAddBulk() {
      this.resetGroupBulk();
      this.getPropertyList();
      this.getSurveyTemplateList();
      this.getSurveyStandardList();
      this.openGroupBulk = true;
      this.titleGroup = "批量添加设备组";
    },

    /** 新增按钮操作 */
    handleAddGroup(row) {
      this.reset();
      // this.getTreeSelect();
      this.getPropertyList();
      this.formGroup.parentId = this.parentIds;
      // if (row != null && row.id) {
      //   this.formGroup.parentId = row.id;
      // } else {
      //   this.formGroup.parentId = 0;
      // }
      this.openGroup = true;
      this.titleGroup = "添加设备组";
    },
    /** 修改按钮操作 */
    handleUpdateGroup(row) {
      this.loading = true;
      this.reset();
      // this.getTreeSelect();
      this.getPropertyList();
      // if (row != null) {
      //   this.formGroup.parentId = row.id;
      // }
      getGroup(row.id).then(response => {
        this.loading = false;
        this.formGroup = response.data;
        this.openGroup = true;
        this.titleGroup = "修改设备组";
      });
    },

    /** 提交按钮 */
    submitFormGroup() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          console.log(this.form)
          if (this.formGroup.parentId == undefined) {
            this.formGroup.parentId = 0;
            this.formGroup.level = 1;
          }
          if (this.formGroup.id != null) {
            updateGroup(this.formGroup).then(response => {
              this.msgSuccess("修改成功");
              this.openGroup = false;
              this.getGroupList();
              this.getTreeSelect();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addGroup(this.formGroup).then(response => {
              this.msgSuccess("新增成功");
              this.openGroup = false;
              this.getGroupList();
              this.getTreeSelect();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDeleteGroup(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认该设备组已无绑定设备?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        return delGroup(ids);
      }).then(() => {
        this.getList();
        this.getTreeSelect();
        this.getGroupList();
        this.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
  }
};
</script>


<style lang="scss" scoped>
.mapSelect {
  position: absolute;
  top: 5px;
  right: 1px;
  background: #fff none repeat scroll 0 0;
  border: 1px solid #ccc;
  margin: 10px auto;
  padding: 6px;
  font-family: "Microsoft Yahei", "微软雅黑", "Pinghei";
  font-size: 14px;
}
</style>

<style lang="scss">
.amap-sug-result {
  z-index: 99999 !important;
}
</style>
